--[[
FilePath: \Lua\View\UIGuild\Skin\UIGuildAllyPanel
战团结盟
--]]

require 'View/UIBase/UIBase'

UIGuildAllyPanel = UIBase.New(PanelResNames.UIGuildAllyPanel)
local _M = UIGuildAllyPanel

_M.hasInit = false

function _M.Init()
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_return"), self.OnBack);
        self.detailCtrl = self:GetController("Ctl_details");
        self.detailCtrl.selectedIndex = 0;
        self.guildListCtrl = self:GetController("Guildlist");
        self.guildListCtrl.selectedIndex = 0;
        self.pageCtrl = self:GetController("page");
        self.pageCtrl.selectedIndex = 0;
        self.inputGuildName = self:GetChild("Input_guildName");
        self.btnSearch = self:GetChild("Btn_search");
        self.listPulish = self:GetChild("list_pulish");
        self.listPulish.itemRenderer = self.RefreshPulishItem;
        self.listApply = self:GetChild("list_apply");
        self.listApply:SetVirtual();
        self.listApply.itemRenderer = self.RefreshApplyItem;
        self.btnRefresh = self:GetChild("Btn_refresh");
        self.btnPulish = self:GetChild("btn_pulish");
        self.btnApply = self:GetChild("btn_apply");
        self.btnHelp = self:GetChild("help");
        --自己公会
        local myGuild = self:GetChild("com_myguild");
        self.my_pageStatusCtrl = myGuild:GetController("pagestatus");
        self.my_icon = myGuild:GetChild("ldr_flag");
        self.my_text_numberofpeople = myGuild:GetChild("Text_numberofpeople");
        self.my_text_lv = myGuild:GetChild("Text_lv");
        self.my_text_guildName = myGuild:GetChild("Text_guildname");
        self.my_text_address = myGuild:GetChild("Text_address");
        self.my_text_combateffectiveness = myGuild:GetChild("Text_combateffectiveness");
        self.my_btn_apply = myGuild:GetChild("Btn_apply");
        self.my_btn_cancel = myGuild:GetChild("Btn_cancel");
        --公会详情
        local guildDetail = self:GetChild("Com_guildDetail");
        self.guildDetailPageCtrl = guildDetail:GetController("page");
        self.guildDetailPageCtrl.selectedIndex = 0;
        self.btn_blackbackground = guildDetail:GetChild("Btn_blackbackground");
        self.Text_Combateffectiveness = guildDetail:GetChild("Text_Combateffectiveness");
        self.text_numberofguildmembers = guildDetail:GetChild("Text_players");
        self.text_lv = guildDetail:GetChild("Text_lv");
        self.text_guildcoin = guildDetail:GetChild("ldr_flag");
        self.text_guildName = guildDetail:GetChild("Text_guildname");
        self.text_number = guildDetail:GetChild("Text_id");
        self.text_address = guildDetail:GetChild("Text_address");
        self.text_leavingamessage = guildDetail:GetChild("Text_notice");
        self.list_memberdetails = guildDetail:GetChild("dynamicList_Listdetails");
        self.list_memberdetails:SetVirtual();
        self.list_memberdetails.itemRenderer = self.RefreshMemberDetailsItem;
        self.btn_refuse = guildDetail:GetChild("btn_refuse");
        self.btn_agree = guildDetail:GetChild("btn_agree");

        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self.btnSearch.onClick:Add(self.onClickSearch, self);
    self.btnRefresh.onClick:Add(self.onClickRefresh, self);
    --self.btnPulish.onClick:Add(self.onClickPulish, self);
    --self.btnApply.onClick:Add(self.onClickApply, self);
    self.pageCtrl.onChanged:Add(self.onChange_page, self);
    self.btn_blackbackground.onClick:Add(self.onClickBG, self);
    self.my_btn_apply.onClick:Add(self.onClickMyApply, self);
    self.my_btn_cancel.onClick:Add(self.onClickMyCancel, self);
    self.btnHelp.onClick:Add(self.onClickHelp, self);

    ListenEvent(Config.EventType.Union_DetailInfo_S2C, self.respUnionInfo, self);
    ListenEvent(Config.EventType.Union_AllianceInfoList_S2C, self.respAllianceInfo, self);
    ListenEvent(Config.EventType.Union_AllianceRelease_S2C, self.respAllianceRelease, self);
    ListenEvent(Config.EventType.Union_AllianceJoin_ClosePanel_S2C, self.respJoinSuc, self);
    ListenEvent(Config.EventType.Union_AllianceJoin_S2C, self.respJoinFail, self);
    ListenEvent(Config.EventType.Union_SearchAlliance_S2C, self.respSearchAlliance, self);

    UIRedDots.addViewListen(PanelResNames.UIGuildAllyPanel, "UIGuildAllyPanel" .. self.btnApply.name, self.btnApply, Config.RedDotsType.GuildAlly);
end

function _M:removeEvent()
    self.btnSearch.onClick:Clear();
    self.btnRefresh.onClick:Clear();
    self.pageCtrl.onChanged:Clear();
    --self.btnPulish.onClick:Clear();
    --self.btnApply.onClick:Clear();
    self.btn_blackbackground.onClick:Clear();
    self.btn_refuse.onClick:Clear();
    self.btn_agree.onClick:Clear();
    self.my_btn_apply.onClick:Clear();
    self.my_btn_cancel.onClick:Clear();
    self.btnHelp.onClick:Clear();

    CloseEvent(Config.EventType.Union_DetailInfo_S2C, self.respUnionInfo, self);
    CloseEvent(Config.EventType.Union_AllianceInfoList_S2C, self.respAllianceInfo, self);
    CloseEvent(Config.EventType.Union_AllianceRelease_S2C, self.respAllianceRelease, self);
    CloseEvent(Config.EventType.Union_AllianceJoin_ClosePanel_S2C, self.respJoinSuc, self);
    CloseEvent(Config.EventType.Union_AllianceJoin_S2C, self.respJoinFail, self);
    CloseEvent(Config.EventType.Union_SearchAlliance_S2C, self.respSearchAlliance, self);

    UIRedDots.removeViewEvent(PanelResNames.UIGuildAllyPanel);
end

function _M:onClickSearch()
    if self.isClick then
        return;
    end
    self.isClick = true;
    local search = string.trim(self.inputGuildName.text);
    if search ~= "" and logicMgr.ChatManager.HasFilterBlockWord(search) then
        self.isClick = false;
        UISysTips.AddMsg(getLanguage("UIFriend_ApplyMsgBlock"));
        return ;
    end
    if #search > 0 then
        local unionId = tonumber(search) or -1;
        local unionInfo = logicMgr.GuildManager.unionInfo;
        if unionId > 0 then
            if tonumber(unionInfo.id) == unionId then
                self.isClick = false;
                return;
            end
        else
            if unionInfo.name == search then
                self.isClick = false;
                return;
            end
        end
        unionHandler.sendReqSearchAlliance(unionId, search);
    else
        self.isClick = false;
    end
end

function _M:onClickRefresh()
    if self.isClick then
        return;
    end
    self.isClick = true;
    if self.isClickCD then
        UISysTips.AddMsg(getLanguage("InvasionBtnCD"));
        self.isClick = false;
        return;
    end
    self.isClickCD = true;
    unionHandler.sendReqAllianceInfoList();
    self:DestroyTimer();
    self.dismissTimer = utimer.timeCall(function()
        self.isClickCD = false;
        self:DestroyTimer();
    end, 3000, 1);
end

function _M:onClickPulish()

end

function _M:onClickApply()

end

function _M:onChange_page(event)
    if self.pageCtrl.selectedIndex == 1 then
        local unionInfo = logicMgr.GuildManager.unionInfo;
        if unionInfo ~= nil then
            self.applyJoins = unionInfo.askJoinList2;
            local num = #self.applyJoins;
            if self.applyJoinNum ~= num then
                self.applyJoinNum = num;
                self.listApply.numItems = num;
            end
            if num < 1 then
                self.guildListCtrl.selectedIndex = 0;
            else
                self.guildListCtrl.selectedIndex = 1;
            end
        else
            self.guildListCtrl.selectedIndex = 0;
        end
        if logicMgr.GuildManager.applyAllyNum > 0 or logicMgr.GuildManager.hasAllyApply then
            logicMgr.GuildManager.applyAllyNum = 0;
            logicMgr.GuildManager.hasAllyApply = false;
            FireEvent(Config.EventType.Union_AllianceMerge_S2C);
        end
    else
        local infos = logicMgr.GuildManager.getAllianceInfoList();
        if infos == nil or #infos < 1 then
            self.guildListCtrl.selectedIndex = 0;
        else
            self.guildListCtrl.selectedIndex = 1;
        end
    end
end

function _M:onClickBG()
    self.detailCtrl.selectedIndex = 0;
end

function _M:onClickMyApply()
    if self.isClickMyItem then
        UISysTips.AddMsg(getLanguage("Sys_Friend_Tips2"));
        return;
    end
    if self.isClick then
        return;
    end
    self.isClick = true;
    self.isClickMyItem = true;
    unionHandler.sendReqAllianceRelease();
    self:DestroyMyItemTimer();
    self.myItemTimer = utimer.timeCall(function()
        self.isClickMyItem = false;
        self:DestroyMyItemTimer();
    end, 5000, 1);
end

function _M:onClickMyCancel()
    if self.isClickMyItem then
        UISysTips.AddMsg(getLanguage("Sys_Friend_Tips2"));
        return;
    end
    if self.isClick then
        return;
    end
    self.isClick = true;
    self.isClickMyItem = true;
    unionHandler.sendReqAllianceCancel();
    self:DestroyMyItemTimer();
    self.myItemTimer = utimer.timeCall(function()
        self.isClickMyItem = false;
        self:DestroyMyItemTimer();
    end, 5000, 1);
end

function _M:onClickHelp()
    logicMgr.UIShowManager.ShowCommonTips("GuildAllyRuleTitle", "GuildAllyRule");
end

-- 显示
function _M:OnEnable()
    if not _M.hasInit then
        self:OnBack();
        return;
    end
    self.isClick = false;
    self.isClickCD = false;
    self.isClickMyItem = false;
    self.myGuildMaxNum = 0;
    self.myGuildNum = 0;
    self.applyJoinNum = 0;
    self:updateMyItemInfo();
    self:updatePulishView();
end

function _M:respAllianceInfo()
    self:updatePulishView();
    self.isClick = false;
end

function _M:respSearchAlliance(msg)
    if msg == nil then
        self.isClick = false;
        return;
    end
    self.allianceInfoList = {};
    if msg.unionOutlineInfos ~= nil and #msg.unionOutlineInfos > 0 then
        self.allianceInfoList = msg.unionOutlineInfos;
        self.guildListCtrl.selectedIndex = 1;
        self.listPulish.numItems = #self.allianceInfoList;
    else
        UISysTips.AddMsg(getLanguage("GuildInvestment_1084"));
        self.inputGuildName.text = "";
    end
    self.isClick = false;
end

function _M:updatePulishView()
    self.allianceInfoList = logicMgr.GuildManager.getAllianceInfoList();
    if self.allianceInfoList == nil or #self.allianceInfoList < 1 then
        self.guildListCtrl.selectedIndex = 0;
        return;
    end
    self.guildListCtrl.selectedIndex = 1;
    self.listPulish.numItems = #self.allianceInfoList;
end

function _M:respAllianceRelease()
    local unionInfo = logicMgr.GuildManager.unionInfo;
    if unionInfo ~= nil then
        local data = unionInfo.unionOutlineInfo;
        if data.release == nil or tonumber(data.release) == 0 then
            self.my_pageStatusCtrl.selectedIndex = 1;
            UISysTips.AddMsg(getLanguage("GuildAlly02"));
        else
            self.my_pageStatusCtrl.selectedIndex = 2;
            UISysTips.AddMsg(getLanguage("GuildAlly01"));
        end
    end
    self.isClick = false;
end

function _M:respJoinSuc(type)
    local selectType = tonumber(type);
    if selectType == 1 then
        self:OnBack();
    elseif selectType == 0 then
        --self:onChange_page();
        --self.guildDetailPageCtrl.selectedIndex = 0;
        --self.detailCtrl.selectedIndex = 0;
        --UISysTips.AddMsg(getLanguage("GuildAllyRefuse"));
    end
    self.isClick = false;
end

function _M:respJoinFail(type)
    if tonumber(type) == 0 then
        self:onChange_page();
        self.guildDetailPageCtrl.selectedIndex = 0;
        self.detailCtrl.selectedIndex = 0;
        UISysTips.AddMsg(getLanguage("GuildAllyRefuse"));
    end
end

function _M:updateMyItemInfo()
    local unionInfo = logicMgr.GuildManager.unionInfo;
    if unionInfo ~= nil then
        local data = unionInfo.unionOutlineInfo;
        self.my_text_guildName.text = data.name;
        local guildLv = tonumber(data.lv);
        self.my_text_lv.text = tostring(guildLv);
        self.myGuildNum = data.memberCount;
        self.myGuildMaxNum = logicMgr.GuildManager.getMaxMemberCnt(guildLv);
        self.my_text_numberofpeople.text = data.memberCount .. "/" .. self.myGuildMaxNum;
        self.my_text_address.text = data.unionSetting.province .. ' ' .. data.unionSetting.city;
        self.my_text_combateffectiveness.text = tostring(data.liveness);
        self.my_icon.url = logicMgr.GuildManager.getFlagUrl(data.unionSetting.flagId);
        if data.release == nil or tonumber(data.release) == 0 then
            self.my_pageStatusCtrl.selectedIndex = 1;
        else
            self.my_pageStatusCtrl.selectedIndex = 2;
        end
    end
end

function _M.RefreshPulishItem(index, itemObj)
    local itemIndex = index + 1;
    _M:upDateItemInfo(_M.allianceInfoList[itemIndex], itemObj, false);
end

function _M.RefreshApplyItem(index, itemObj)
    local itemIndex = index + 1;
    _M:upDateItemInfo(_M.applyJoins[itemIndex], itemObj, true);
end

function _M:upDateItemInfo(data, obj, isApply)
    local pageStatusCtrl = obj:GetController("pagestatus");
    local icon = obj:GetChild("ldr_flag");
    local text_numberofpeople = obj:GetChild("Text_numberofpeople");
    local text_lv = obj:GetChild("Text_lv");
    local text_guildName = obj:GetChild("Text_guildname");
    local text_address = obj:GetChild("Text_address");
    local text_combateffectiveness = obj:GetChild("Text_combateffectiveness");
    local btn_Join = obj:GetChild("Btn_Join");
    local btn_refuse = obj:GetChild("btn_refuse");
    local btn_agree = obj:GetChild("btn_agree");
    btn_Join.onClick:Clear();
    btn_refuse.onClick:Clear();
    btn_agree.onClick:Clear();
    icon.onClick:Clear();
    text_guildName.text = data.name;
    local guildLv = tonumber(data.lv);
    text_lv.text = tostring(guildLv);
    local maxNum = logicMgr.GuildManager.getMaxMemberCnt(guildLv);
    local memberNum = data.memberCount;
    --local maxCount = math.max(maxNum, self.myGuildMaxNum);
    text_numberofpeople.text = memberNum.."/"..maxNum;
    text_address.text = data.unionSetting.province .. ' ' .. data.unionSetting.city;
    text_combateffectiveness.text = tostring(data.liveness);
    icon.url = logicMgr.GuildManager.getFlagUrl(data.unionSetting.flagId);
    local allianceId = tonumber(data.id);
    icon.onClick:Add(function()
        if self.isClick then
            return ;
        end
        self.isClick = true;
        if allianceId > 0 then
            unionHandler.reqUnionByIdInfo(allianceId);
        else
            self.isClick = false;
        end
    end);
    if isApply then
        pageStatusCtrl.selectedIndex = 3;
        btn_refuse.onClick:Add(function()
            self:sendReqHandleAllianceJoin(false, allianceId);
        end);
        btn_agree.onClick:Add(function()
            if (self.myGuildNum + memberNum) > self.myGuildMaxNum then
                UISysTips.AddMsg(getLanguage("GuildAllyNum"));
                return;
            end
            self:sendReqHandleAllianceJoin(true, allianceId);
        end);
    else
        pageStatusCtrl.selectedIndex = 0;
        btn_Join.onClick:Add(function()
            if self.isClick then
                return;
            end
            self.isClick = true;
            if allianceId > 0 then
                unionHandler.sendReqAllianceMerge(allianceId);
                UISysTips.AddMsg(getLanguage("GuildAllyPulish"));
            end
            pageStatusCtrl.selectedIndex = 4;
            self.isClick = false;
        end);
    end
end

function _M:sendReqHandleAllianceJoin(isAgree, id)
    if self.isClick then
        return ;
    end
    self.isClick = true;
    if isAgree then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
        pop.Init(110, function (flag)
            if flag then
                unionHandler.sendReqHandleAllianceJoinList(1, id);
            else
                self.isClick = false;
            end
        end);
    else
        unionHandler.sendReqHandleAllianceJoinList(0, id);
    end
end

function _M:respUnionInfo(msg)
    if msg.unionInfo then
        self.detailCtrl.selectedIndex = 1;
        local data = msg.unionInfo.unionOutlineInfo;
        self.Text_Combateffectiveness.text = tostring(data.liveness)
        local guildLv = tonumber(data.lv);
        local memberNum = data.memberCount;
        --local maxCount = logicMgr.GuildManager.getMaxMemberCnt(guildLv);
        self.text_numberofguildmembers.text = data.memberCount.."/"..logicMgr.GuildManager.getMaxMemberCnt(guildLv);
        self.text_lv.text = tostring(guildLv);
        self.text_guildcoin.url = logicMgr.GuildManager.getFlagUrl(data.unionSetting.flagId);
        self.text_guildName.text = data.name;
        self.text_number.text = tostring(data.id);
        self.text_address.text = getLanguage("GuildInvestment_1145") .. logicMgr.GuildManager.getUnionAddress(msg.unionInfo);
        self.text_leavingamessage.text = data.unionSetting.unionNotice or "";
        self.guildDetailPageCtrl.selectedIndex = 0;
        self.btn_refuse.onClick:Clear();
        self.btn_agree.onClick:Clear();
        self.guildMembers = msg.unionInfo.members;
        self.list_memberdetails.numItems = #self.guildMembers;
        if self.pageCtrl.selectedIndex == 1 then
            self.guildDetailPageCtrl.selectedIndex = 1;
            local allianceId = tonumber(data.id)
            self.btn_refuse.onClick:Add(function()
                self:sendReqHandleAllianceJoin(false, allianceId);
            end);
            self.btn_agree.onClick:Add(function()
                if (self.myGuildNum + memberNum) > self.myGuildMaxNum then
                    UISysTips.AddMsg(getLanguage("GuildAllyNum"));
                    return;
                end
                self:sendReqHandleAllianceJoin(true, allianceId);
            end);
        end


        self.isClick = false;
    else
        self.isClick = false;
    end
end

function _M.RefreshMemberDetailsItem(index, itemObj)
    local position = itemObj:GetController("position");
    local state = itemObj:GetController("state");
    local btn_head = itemObj:GetChild("btn_head");
    local btn_active = itemObj:GetChild("Btn_active");
    local text_lvName = itemObj:GetChild("Text_lvName");
    local text_lv = itemObj:GetChild("Text_lv");
    local text_name = itemObj:GetChild("Text_name");
    local text_power = itemObj:GetChild("Text_power");
    local text_deputyhead = itemObj:GetChild("Text_deputyhead");
    local text_president = itemObj:GetChild("Text_president");
    text_president.text = getLanguage("GuildInvestment_1053");
    text_deputyhead.text = getLanguage("GuildInvestment_1054");
    local text_online = itemObj:GetChild("Text_online");
    local frame = btn_head:GetChild("frame");
    local icon = btn_head:GetChild("head"):GetChild("HeadIcon");

    local data = _M.guildMembers[index + 1];
    text_name.text = data.showInfo.name;
    text_power.text = getNumString(toNumber(tostring(data.showInfo.maxPower)));
    btn_active.text = tostring(data.livenessWeek);
    text_lv.text = data.showInfo.level;
    frame.url = "ui://ICONAtlas/" .. data.showInfo.picFrame;
    icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(data.showInfo.pic) --"ui://ICONAtlas/" .. dataTable.getRow("t_hero", data.showInfo.pic)["f_HeroIcon"]
    local selIdx = 2;
    if data.jobId == logicMgr.GuildManager.JOB_VICECHAIRMAN_ID then
        selIdx = 1;
    elseif data.jobId == logicMgr.GuildManager.JOB_CHAIRMAN_ID then
        selIdx = 0;
    end
    position.selectedIndex = selIdx;
    state.selectedIndex = 0;
    if data.offlineTime then
        local offlineTime = tonumber(tostring(data.offlineTime));
        if offlineTime > 0 then
            text_online.text = getLanguage("GuildInvestment_1076") .. getTimeStrType2(offlineTime);
            state.selectedIndex = 1;
        else
            text_online.text = getLanguage("GuildInvestment_1077");
        end
    else
        text_online.text = getLanguage("GuildInvestment_1077");
    end
end

-- 隐藏
function _M:OnDisable()

end

function _M:DestroyTimer()
    if self.dismissTimer ~= nil then
        utimer.remove(self.dismissTimer);
        self.dismissTimer = nil;
    end
end

function _M:DestroyMyItemTimer()
    if self.myItemTimer ~= nil then
        utimer.remove(self.myItemTimer);
        self.myItemTimer = nil;
    end
end

-- 卸载
function _M:OnDestroy()
    self:DestroyTimer();
    self:DestroyMyItemTimer();
    self:removeEvent();
    self.hasInit = false;
end

return _M;